#include <COMMON_HEADER/common_header.h>
/*
 * Copyright (c) 2006-2020, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-11-27     RT-Thread    first version
 */
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG

#define THREAD_PRIORITY         25
#define THREAD_STACK_SIZE       512
#define THREAD_TIMESLICE        5

ALIGN(RT_ALIGN_SIZE)
//min stack :2048KB
static char thread1_stack[2048];
static struct rt_thread thread1;

static char thread2_stack[4096];
static struct rt_thread thread2;

static char thread3_stack[512];
static struct rt_thread thread3;

static char thread5_stack[1024];
static struct rt_thread thread5;

static char thread6_stack[3600];
static struct rt_thread thread6;

static char thread7_stack[512];
static struct rt_thread thread7;
//
//static char thread8_stack[1024];
//static struct rt_thread thread8;
//
//static char thread9_stack[1024];
//static struct rt_thread thread9;

///*socket1*/
//static char thread10_stack[2048];
//static struct rt_thread thread10;
//
///*socket2*/
//static char thread11_stack[2048];
//static struct rt_thread thread11;
//
//static char thread12_stack[512];
//static struct rt_thread thread12;

int main(void)
{
    /***********************硬件外设初始化******************************/
    //挂载文件系统
    spi_flash_mount();
    dtu_config_file_init();
    //看门狗运行
    gpio_init();
    rs485_uart_init();
    rs232_uart_init();
    led_init();
    watch_dog_init();
    //debug_uart_init();

    //register_messagequeue_init();

    /***********************硬件外设初始化******************************/

    //创建任务线程1：watchdog运行线程
    //初始化线程1，名称是thread1，入口函数是watchdog_thread
    rt_thread_init(&thread1,
                  "Watchdog",
                  watchdog_thread,
                  RT_NULL,
                  &thread1_stack[0],
                  sizeof(thread1_stack),
                  THREAD_PRIORITY - 3, THREAD_TIMESLICE);
    rt_thread_startup(&thread1);

    rt_thread_init(&thread2,
                  "Network",
                  network_connect_thread,
                  RT_NULL,
                  &thread2_stack[0],
                  sizeof(thread2_stack),
                  THREAD_PRIORITY - 2, THREAD_TIMESLICE);
    rt_thread_startup(&thread2);

    rt_thread_init(&thread3,
                  "Led3",
                  network_led_thread_entry,
                  RT_NULL,
                  &thread3_stack[0],
                  sizeof(thread3_stack),
                  THREAD_PRIORITY - 1, THREAD_TIMESLICE);
    rt_thread_startup(&thread3);

//    rt_thread_init(&thread5,
//                  "heartbeat",
//                  heartbeat_thread_entry,
//                  RT_NULL,
//                  &thread5_stack[0],
//                  sizeof(thread5_stack),
//                  THREAD_PRIORITY - 1, THREAD_TIMESLICE);
//    rt_thread_startup(&thread5);

//    rt_thread_init(&thread6,
//                  "thread6",
//                  debug_serial_thread_entry,
//                  RT_NULL,
//                  &thread6_stack[0],
//                  sizeof(thread6_stack),
//                  THREAD_PRIORITY - 2, THREAD_TIMESLICE);
//    rt_thread_startup(&thread6);

    rt_thread_init(&thread7,
                 "Led1",
                 connect_led_thread_entry,
                 RT_NULL,
                 &thread7_stack[0],
                 sizeof(thread7_stack),
                 THREAD_PRIORITY - 1, THREAD_TIMESLICE);
    rt_thread_startup(&thread7);

//    rt_thread_init(&thread8,
//                    "rs485serial",
//                    rs485_serial_thread_entry,
//                    RT_NULL,
//                    &thread8_stack[0],
//                    sizeof(thread8_stack),
//                    THREAD_PRIORITY - 2, THREAD_TIMESLICE);
//    rt_thread_startup(&thread8);

//    rt_thread_init(&thread9,
//                    "rs232serial",
//                    rs232_serial_thread_entry,
//                    RT_NULL,
//                    &thread9_stack[0],
//                    sizeof(thread9_stack),
//                    THREAD_PRIORITY - 2, THREAD_TIMESLICE);
//    rt_thread_startup(&thread9);
//
//    rt_thread_init(&thread10,
//                    "socket1",
//                    socket_recv_thread1_entry,
//                    RT_NULL,
//                    &thread10_stack[0],
//                    sizeof(thread10_stack),
//                    THREAD_PRIORITY - 3, THREAD_TIMESLICE);
//    rt_thread_startup(&thread10);
//
//    rt_thread_init(&thread11,
//                   "socket2",
//                   socket_recv_thread2_entry,
//                   RT_NULL,
//                   &thread11_stack[0],
//                   sizeof(thread11_stack),
//                   THREAD_PRIORITY - 3, THREAD_TIMESLICE);
//   rt_thread_startup(&thread11);

//   rt_thread_init(&thread12,
//                  "socket3",
//                  socket_recv_thread3_entry,
//                  RT_NULL,
//                  &thread12_stack[0],
//                  sizeof(thread12_stack),
//                  THREAD_PRIORITY - 3, THREAD_TIMESLICE);
//  rt_thread_startup(&thread12);
    //void SystemInit (void)
    return RT_EOK;
}
